findRange
, which, when applied to a string, will search for the first occurrence of a match to the supplied string. findRange self toMatchThis function finds the first range of contiguous values inside the string self that matches the characters in the string toMatch. It returns the ordinal position of the first character in the matching range or 0 if there is no match.
global myStr := "Let's see how this works."
findRange myStr "see how this works"
7
findRange myStr "see if this works"
0
The global function findNthContext
, which provide searching capabilities
specific to strings, is useful for parsing. It allows you to search for the nth
word, sentence, or paragraph. You can also supply a function designating a
delimiter and search for the range of characters bounded by that delimiter.
findNthContext args contextThe argument args is a
Quad
which supplies the following :
The following example demonstrates using findNthContext
but is not a complete explanation. More information is available in the global functions section of ScriptX Class Reference.
-- create a string
global str := "This is a sample string."
-- search for the fourth word in str, starting at the cursor position -- 0 and continuing until the end (str.size)
global args := #(str, 4, 0, str.size) as Quad
findNthContext args @word
-- print out the word returned
copyFromTo args[1] args[3] args[4]
"sample"-- search for the second set of characters bounded by ":"
global s := "first name:last name:street address:city:state"
global args := #(s, 2, 0, s.size) as Quad
findNthContext args (r -> r == ":"[1])
-- print out the characters returned
copyFromTo args[1] args[3] args[4]
"last name:"A second global function for searching strings is
searchIndex
, which finds the first occurrence of the string you want to match. (Note that the string you want to match must be at least three characters long and cannot contain any spaces.) Searching is fast because searchIndex
is actually searaching a signature index, which is built automatically when you create a StringIndex
object and supply a string for its string
instance variable.searchIndex strIndex match searchContext wholeWordThis function searches the
StringIndex
object strIndex for the first occurrence of match using searchContext to tell it where to search. The last argument, wholeWord, is either true
or false
, indicating whether the match must be a whole word (as opposed to only part of a word).
The following code example demonstrates using searchIndex
. It will be easier to follow if you have read the entries for SearchContext
and StringIndex
in ScriptX Class Reference.
global myString := "Newton devised a new system."
-- create a StringIndex object
global strIndex := new StringIndex string:myString
-- create a SearchContext object suitable for beginning the search at -- the beginning of the string
global sc := initialSearchContext strIndex 0
-- search for the first occurrence of "new" as a whole word
sc := searchIndex strIndex "new" sc true
-- print out the result
copyFromTo sc.string sc.startOffset sc.endOffset
"new"
-- search for the first occurrence of "new" as a whole word or as part -- of a larger word. We need to create a new SearchContext object to
-- start the search at the beginning of the string.
sc := initialSearchContext strIndex 0
sc := searchIndex strIndex "new" sc false
-- print out the result (Note that searchIndex is not case sensitive.)
copyFromTo sc.string sc.startOffset sc.endOffset
"New"
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.